Skip to main content

Flow diagrams - sankey with more than one transition

The script below demonstrates how to create flowcharts that show transitions between more than two states. This enables you to follow individual movements over longer periods of time and with more frequent measurements.

 require no.ssb.fdb:30 as db

create-dataset sankeydata
import db/BEFOLKNING_FOEDSELS_AAR_MND as birthdate
generate age = 1992 - int(birthdate/100)
keep if inrange(age,18,20)
import db/BOSATTEFDT_BOSTED 1992-01-01 as municipality92
import db/BOSATTEFDT_BOSTED 2002-01-01 as municipality02
import db/BOSATTEFDT_BOSTED 2012-01-01 as municipality12
import db/BOSATTEFDT_BOSTED 2022-01-01 as municipality22

generate county92 = substr(municipality92,1,2)
generate county02 = substr(municipality02,1,2)
generate county12 = substr(municipality12,1,2)
generate county22 = substr(municipality22,1,2)

sankey county92 county02 county12 county22 

// Note that the municipality codes changed a lot from 2020, hence the large changes for county22. You can either recode the county codes so they match, or you can create coarser divisions as below (regions).

generate region22 = 1 if inlist(county22,'03') //Oslo
replace region22 = 2 if inlist(county22,'30','34','38') //Østlandet
replace region22 = 3 if inlist(county22,'11','42') //Sørlandet og Rogaland
replace region22 = 4 if inlist(county22,'46') //Vestlandet
replace region22 = 5 if inlist(county22,'15','50') //Midtnorge
replace region22 = 6 if inlist(county22,'18','54') //Nordnorge

generate region12 = 1 if inlist(county12,'03') //Oslo
replace region12 = 2 if inlist(county12,'01','02','04','05','06','07','08') //Østlandet
replace region12 = 3 if inlist(county12,'09','10','11') //Sørlandet og Rogaland
replace region12 = 4 if inlist(county12,'12','14') //Vestlandet
replace region12 = 5 if inlist(county12,'15','16','17') //Midtnorge
replace region12 = 6 if inlist(county12,'18','19','20') //Nordnorge

generate region02 = 1 if inlist(county02,'03') //Oslo
replace region02 = 2 if inlist(county02,'01','02','04','05','06','07','08') //Østlandet
replace region02 = 3 if inlist(county02,'09','10','11') //Sørlandet og Rogaland
replace region02 = 4 if inlist(county02,'12','14') //Vestlandet
replace region02 = 5 if inlist(county02,'15','16','17') //Midtnorge
replace region02 = 6 if inlist(county02,'18','19','20') //Nordnorge

generate region92 = 1 if inlist(county92,'03') //Oslo
replace region92 = 2 if inlist(county92,'01','02','04','05','06','07','08') //Østlandet
replace region92 = 3 if inlist(county92,'09','10','11') //Sørlandet og Rogaland
replace region92 = 4 if inlist(county92,'12','14') //Vestlandet
replace region92 = 5 if inlist(county92,'15','16','17') //Midtnorge
replace region92 = 6 if inlist(county92,'18','19','20') //Nordnorge

define-labels regiontxt 1 Oslo 2 Østlandet 3 "Sørlandet og Rogaland" 4 Vestlandet 5 "Midt-Norge" 6 "Nord-Norge"
assign-labels region92 regiontxt
assign-labels region02 regiontxt
assign-labels region12 regiontxt
assign-labels region22 regiontxt

sankey region92 region02 region12 region22


create-dataset sankeydata2
import db/BEFOLKNING_FOEDSELS_AAR_MND as birthdate
generate age = 2015 - int(birthdate/100)
keep if inrange(age,18,67)
import db/REGSYS_ARB_ARBMARK_STATUS 2015-11-16 as workstatus15
import db/REGSYS_ARB_ARBMARK_STATUS 2019-11-16 as workstatus19
import db/REGSYS_ARB_ARBMARK_STATUS 2023-11-16 as workstatus23

recode workstatus15 workstatus19 workstatus23 ("2" = "1")
define-labels workstatustxt "0" "Outside labor force" "1" "In job" "3" "Fully unemployed"
assign-labels workstatus15 workstatustxt
assign-labels workstatus19 workstatustxt
assign-labels workstatus23 workstatustxt

sankey workstatus15 workstatus19 workstatus23
sankey workstatus15 workstatus19 workstatus23 if inrange(age,18,25)
sankey workstatus15 workstatus19 workstatus23 if inrange(age,26,50)
sankey workstatus15 workstatus19 workstatus23 if inrange(age,60,67)


create-dataset sankeydata3
import db/BEFOLKNING_FOEDSELS_AAR_MND as birthdate
generate age = 1992 - int(birthdate/100)
keep if inrange(age,18,67)
import db/SIVSTANDFDT_SIVSTAND 1992-01-01 as marital_status92
import db/SIVSTANDFDT_SIVSTAND 2002-01-01 as marital_status02
import db/SIVSTANDFDT_SIVSTAND 2012-01-01 as marital_status12
import db/SIVSTANDFDT_SIVSTAND 2022-01-01 as marital_status22
drop if inlist("0",marital_status92,marital_status02,marital_status12,marital_status22)

recode marital_status92 marital_status02 marital_status12 marital_status22 ("5" "7" "8" = "4" "Divorced/separated")("6" = "2" "Married/partner")("9" = "3")

sankey marital_status92 marital_status02 marital_status12 marital_status22